package com.ujiuye.servlet;

import com.ujiuye.bean.AdminBean;
import com.ujiuye.service.SystemService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/login")
public class SystemServlet extends HttpServlet {
    private SystemService ss=new SystemService();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理中午乱码问题
//        resp.setContentType("text/html;charset=utf-8");
        //获取页面数据
        String uname=req.getParameter("uname");
        String pwd=req.getParameter("pwd");
        //调用service层的登录方法
        AdminBean admin = ss.login(uname, pwd);
        //创建输出页面数据
        PrintWriter out= resp.getWriter();
        if(admin!=null){
            //成功
            //将用户信息保存到cookie
            Cookie username = new Cookie("uname", uname);
            Cookie password = new Cookie("pwd", pwd);
            username.setMaxAge(60*60);
            password.setMaxAge(60*60);
            resp.addCookie(username);
            resp.addCookie(password);
//            //将用户信息保存到session中
            HttpSession session = req.getSession();
            session.setAttribute("admin",admin );
//            out.print("<h2 style='color:green'>登录成功</h2>");
            //跳转至显示学员列表页面
            resp.sendRedirect("student?method=findAllStudent");
        }else {
            //失败
            out.print("<h2 style='color:red'>登录失败</h2>");
        }

    }
}

